﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BattleThis.Core;

namespace BattleThis.Controllers
{
	[Authorize]
    public class UserController : Controller
    {

		#region Variablen
		public const string EditAction = "Edit";
		public static readonly ReflectedControllerDescriptor Info = new ReflectedControllerDescriptor( typeof( UserController ) );
		#endregion

		#region Properties
		#endregion

		#region Methoden

		[HttpGet, ActionName( EditAction ), Persistence]
		public ActionResult Edit()
		{
			
			var repository = this.HttpContext.GetRepository<User>();
			var user = repository.Find( x => x.Name.Equals( HttpContext.User.Identity.Name ) ).Single();

			return View(user);
		}

		[HttpPost, ActionName( EditAction ),Persistence]
		public ActionResult Edit( string name )
		{

			var repository = this.HttpContext.GetRepository<User>();
			var user = repository.Find( x => x.Name.Equals( HttpContext.User.Identity.Name ) ).Single();

			if( !this.ModelState.IsValid )
				return View( user );

			user.Name = name;
			repository.SaveChanges();

			return this.RedirectToAction(  LobbyController.IndexAction, LobbyController.Info.ControllerName );
		}

		#endregion

        

    }
}
